package com.it.jzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.jzs.dao.UserCollectionMapper;
import com.it.jzs.dao.admin.AdminBlogMapper;
import com.it.jzs.pojo.Blog;
import com.it.jzs.pojo.UserCollection;
import com.it.jzs.service.UserCollectionService;
import com.it.jzs.vo.BlogCollectionVo;
import com.it.jzs.vo.BlogCommentVo;
import com.it.jzs.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserCollectionServiceImpl implements UserCollectionService {
    private static final Integer SHOUCHANGQUXAO = 0; // 收藏取消
    private static final Integer YIJINGSHOUCHANG = 1;  // 收藏成功
    private static final Integer FAIL = -1;
    @Resource
    private UserCollectionMapper collectionMapper;

    @Resource
    private AdminBlogMapper blogMapper;

    @Override
    public Result<Object> insert(Integer id, Integer userId) {
        QueryWrapper<UserCollection> wrapper = new QueryWrapper<>();
        Blog blog = blogMapper.selectById(id); // 先通过博客id查出博客
        wrapper.eq("user_id", userId);
        wrapper.eq("blog_id", id);
        UserCollection userCollection = collectionMapper.selectOne(wrapper);
        if (userCollection == null) {  // 说明此用户还没收藏过，插入一条记录
            UserCollection collection = new UserCollection();
            collection.setBlogId(id);
            collection.setCreateTime(System.currentTimeMillis());
            collection.setUserId(userId);
            collection.setBlogUserId(blog.getUserId());
            collection.setIsCollection(YIJINGSHOUCHANG);
//            collection.set
            int insert = collectionMapper.insert(collection);
            if (insert > 0) {
                blog.setCollection(blog.getCollection() + 1);
                blogMapper.updateById(blog);  // 修改收藏数
                return Result.success(1, "收藏成功");
            }
            return Result.error(-1, "收藏失败");
        } else {  // 收藏过
            Integer isCollection = userCollection.getIsCollection();  // 取出来收藏的状态
            isCollection = isCollection.equals(SHOUCHANGQUXAO) ? YIJINGSHOUCHANG : SHOUCHANGQUXAO;    // 如果是0表示没有收藏，收藏取消，1表示已经收藏
            userCollection.setIsCollection(isCollection);
            int i = collectionMapper.updateById(userCollection);
            if (i > 0 && isCollection.equals(SHOUCHANGQUXAO)) {  // 取消收藏成功
                blog.setCollection(blog.getCollection() - 1);
                blogMapper.updateById(blog);
                return Result.success(SHOUCHANGQUXAO, "取消收藏成功");
            } else if (i > 0 && isCollection.equals(YIJINGSHOUCHANG)) {
                blog.setCollection(blog.getCollection() + 1);
                blogMapper.updateById(blog);
                return Result.success(YIJINGSHOUCHANG, "收藏成功");
            }
        }
        return Result.success(YIJINGSHOUCHANG, "收藏成功");
    }

    @Override  // 博客id 用户id
    public Result<Object> selectCollectionStatus(Integer id, Integer userId) {
//        System.out.println("id = " + id);
//        System.out.println("userId = " + userId);
//        QueryWrapper<UserCollection> wrapper = new QueryWrapper<>();
//        wrapper.eq("blog_id", id).and(wrapper -> {
//            wrapper.eq('userId', userId)
//        });

//        UserCollection userCollection = collectionMapper.selectOne(wrapper);
        UserCollection userCollection = collectionMapper.selectCollectionStatus(id, userId);
        if (userCollection==null){  // 说明这个用户没有收藏  要不然会空指针异常，
            return Result.success(0, "获取点赞状态");

        }
//        System.out.println("userCollection = " + userCollection);
        return Result.success(userCollection.getIsCollection(), "获取点赞状态");

    }

    @Override
    public Result<Object> selectCollection(Integer userId) {
        QueryWrapper<UserCollection> wrapper = new QueryWrapper<>();
        List<BlogCollectionVo> blogCollectionVos = collectionMapper.selectCollection(userId);
        for (BlogCollectionVo blogCollectionVo : blogCollectionVos) { // 时间戳转换
            Long createTime = blogCollectionVo.getCreateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(createTime);
            String format = simpleDateFormat.format(date);
            blogCollectionVo.setFormatTime(format);
        }
        return Result.success(blogCollectionVos, "获取收藏数据成功");

    }
}
